 $(document).ready(function() {
	//获取cookie:ueserid & Wtid
	var invoiceid = $.cookie("invoiceid");
	console.log("invoiceid:"+invoiceid);
	var editcookie = $.cookie("nav");
	var Wtid = JSON.parse(editcookie).w;

	//填充数据
	var searchurl = dataport + '/MaterialInvoice/selectById'; //查询
	$.post(searchurl, {
		'invoiceId': invoiceid,
	}, function(result) {
		var code = result.code;
		var editlist = result.object;
		console.log(editlist.name);
		if(code == 0) {
			$("#L_name").val(editlist.name); //用户名
			$("#L_type").val(editlist.type); //手机号码
			$("#L_description").val(123); //手机号码
		} else {
			console.log("网络不佳，稍后再试！")
		}
	});
	
	//下拉菜单选项
	var selected =(function(){
		var value = $("#L_type").val();
		console.log(value);
		$('#L_type option[value="'+value+'"]').attr("selected",true);
		var a =$('#L_type option[value="'+value+'"]').attr("id");
		console.log(a);
	});


	//layui
	layui.use(['form', 'layer'], function() {
		$ = layui.jquery;
		var form = layui.form,
			layer = layui.layer;

		//自定义验证规则
		form.verify({
			username: function(value) {
				if(value.length < 5) {
					return '昵称至少得5个字符啊';
				}
			},
			password: [/(.+){6,12}$/, '密码必须6到12位'],
			id_card: function(value) {
				if(value.length != 18) {
					return '请输入正确的18位身份证号码';
				}
			}
		});

		//获取表单数据
		$("input").blur(function() {
			//失去焦点事件
			var name = $("#L_name").val(); //单据名
			var type = $("#L_type").val(); //类型
			var description = $("#L_description").val(); //描述
			//监听提交
			form.on('submit(add)', function() {
				//递交修改
				editData(name, type, description);
				return false;
			});
		});

		//修改用户信息
		var editurl = dataport + '/MaterialInvoice/update'; //修改
		// 获得frame索引
		var index = parent.layer.getFrameIndex(window.name);
		var editData = function(name, type, description) {
			//修改 edit
			$.post(editurl, {
				invoiceid: invoiceid,
				name: name,
				type:type,
				description:description
			}, function(result) {
				var code = result.code;
				if(code == 0) {
					layer.msg('修改成功!', {
						icon: 1,
						time: 1000
					}, function() {
						//关闭当前frame
						parent.layer.close(index);
					});
				} else {
					layer.msg('网络开小差了，请稍后再试!', {
						icon: 2,
						time: 1000
					}, function() {
						//关闭当前frame
//						parent.layer.close(index);
					});
				}
			})
		}
	});
})